<#include "layout/template.ftl"/>

<@htmlHead title="修改商品">
	<link rel="stylesheet" href="${resources}/css/fileupload.css" />
   	<link  rel="stylesheet" href="${resources}/css/upload.multiple.css" >
</@htmlHead>


<@htmlBody 'commodity' 'goods'>

	<!-- breadcrumbs begin -->
	<#include "layout/breadcrumbs.ftl">
	<@breadcrumbs '商品' '修改'></@breadcrumbs>
	<!-- breadcrumbs end -->

	<!-- page-content begin -->
	<div class="page-content">
			
			<!-- page-header begin -->
			<div class="page-header">
					<h1>
						修改
					</h1>
			</div>
			<!-- page-header end -->

			<!-- 表单开始 -->
			<form class="form-horizontal" id="validation-form" method="post" action="${path}/manage/commodity/goods/update/${goods.id}" >
				
				
			<!-- row begin -->
			<div class="row">	
				<div class="col-md-8 col-md-offset-2">
							<input type="hidden" name="merchant.id" value="${goods.merchant.id}"  >
							<input type="hidden" name="status" value="${goods.status}"  >
							<input type="hidden" name="sales" value="${goods.sales}"  >
							<input type="hidden" name="createTime" value="${goods.createTime}"  >
							<input type="hidden" name="goodsVirtual.id" value="${goods.goodsVirtual.id}"  >

							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname">商品名称:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<input type="text" class="form-control" placeholder="请填写商品名称" name="name"   id="name" value="${goods.name}" style="width:50%"	 />
									</div>
								</div>
							</div>
								
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname">标题:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<input type="text" class="form-control" placeholder="请输入标题" name="title"   id="title" value="${goods.title}" style="width:50%" />
									</div>
								</div>
							</div>
						 <input type="hidden" id="limitType" name="limitType" value="${goods.limitType}"  >
							<#if market.id!=1>
							<div class="form-group">	
								<label class="control-label col-md-2 no-padding-right" for="cpname">商品子分类:</label>
								<div class="col-md-10">
									<div class="clearfix">
											<select id="categoryid" name="category.id" style="width: 50%">
										
											<#list categoryList as category>
												<option value="${category.id}"  ${(goods.category.id==category.id)?string('selected','')}>${category.name}</option>
											</#list>
										</select>
									</div>
								</div>
							</div>
							<#else>
							<div class="form-group">	
								<label class="control-label col-md-2 no-padding-right" for="cpname">商品父分类:</label>
								<div class="col-md-10">
									<div class="clearfix">
											<select id="categoriesid" name="categories.id" style="width: 50%">
											
											<#list categoryLists as categories>
												<option value="${categories.id}"  ${(categories.id==goods.category.pcategory.id)?string('selected','')}>${categories.name}</option>
											</#list>
										</select>
									</div>
								</div>
							</div>
							
							<div class="form-group">	
								<label class="control-label col-md-2 no-padding-right" for="cpname">商品子分类:</label>
								<div class="col-md-10">
									<div class="clearfix">
											<select id="categoryid" name="category.id" style="width: 50%">
										
											<#list categoryList as category>
												<option value="${category.id}"  ${(goods.category.id==category.id)?string('selected','')}>${category.name}</option>
											</#list>
										</select>
									</div>
								</div>
							</div>
							</#if>
							
							<!-- 价格相关 -->
							<div class="hr hr-dotted" style="border-top: 2px dotted #AAA"></div>
							
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname">原价:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<input type="text" class="form-control" placeholder="请输入原价" name="originalPrice"   id="originalPrice" value="${goods.originalPrice}"  style="width:50%"/>
									</div>
								</div>
							</div>
						
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname">促销价:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<input type="text" class="form-control" placeholder="请输入促销价" name="promotionPrice"   id="promotionPrice" value="${goods.promotionPrice}"   style="width:50%" />
									</div>
								</div>
							</div>
						
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname">价格:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<input type="text" class="form-control" placeholder="请输入价格" name="price"   id="price" value="${goods.price}"     style="width:50%" />
									</div>
								</div>
							</div>
						
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname" >是否特价: </label> 
								<div class="col-md-10">
									<label class="radio-inline">
								  		<input type="radio" name="isCheap" value="1"  ${(goods.isCheap==1)?string('checked','')}> 是
									</label>
									<label class="radio-inline">
								  		<input type="radio" name="isCheap" value="0" ${(goods.isCheap==0)?string('checked','')} > 否
									</label>
								</div>
							</div>
							
							<!--
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname" >是否打折: </label> 
								<div class="col-md-10">
									<label class="radio-inline">
								  		<input type="radio" name="isDiscount" value="1" ${(goods.isDiscount==1)?string('checked','')}> 是
									</label>
									<label class="radio-inline">
								  		<input type="radio" name="isDiscount" value="0" ${(goods.isDiscount==0)?string('checked','')}> 否
									</label>
									<input type="number" class="form-control"  name="discount"   id="discount" value="${goods.discount}"  style="width:50%;display:${(goods.isDiscount==0)?string('none','block')};"  />
								</div>
							</div>
							-->
						 	<input type="hidden" name="isDiscount" value="0">
						 	<input type="hidden" name="discount"  value="${goods.discount}" />
						
							<!-- 促销相关，虚拟商品相关 -->
							<div class="hr hr-dotted" style="border-top: 2px dotted #AAA"></div>
						<!-- 限天超商户 -->
                      <#if merchant.type==0> 		
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname">启用库存:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<label class="radio-inline">
									  		<input type="radio" name="isInventory" value="1"  ${(goods.isInventory==1)?string('checked','')}> 是
										</label>
										<label class="radio-inline">
									  		<input type="radio" name="isInventory" value="0"  ${(goods.isInventory==0)?string('checked','')}> 否
										</label>
										<input type="text" class="form-control" placeholder="请输入库存剩余数" name="remain"   id="remain" value="${goods.remain}"  style="width:50%;display:${(goods.isInventory==0)?string('none','block')};"  />
									</div>
								</div>
							</div>
					<#else>
                         <div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname">启用库存:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<label class="radio-inline">
									  		<input type="radio" name="isInventory" value="1"  ${(goods.isInventory==1)?string('checked','')}> 是
										</label> 
										<input type="text" class="form-control" placeholder="请输入库存剩余数" name="remain"   id="remain" value="${goods.remain}"  style="width:50%;"  />
									</div>
								</div>
							</div>

                    </#if> 
                    <div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname">启用限购:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<label class="radio-inline">
									  		<input type="radio" name="isPurRestrictions" value="1"  ${(goods.isPurRestrictions==1)?string('checked','')}> 是
										</label>
										<label class="radio-inline">
									  		<input type="radio" name="isPurRestrictions" value="0"  ${(goods.isPurRestrictions==0)?string('checked','')}> 否
										</label>
										<input type="text" class="form-control" placeholder="请输入限购数量" name="purRestrictionsNum"   id="purRestrictionsNum" value="${goods.purRestrictionsNum}"  style="width:50%;display:${(goods.isPurRestrictions==0)?string('none','block')};"  />
									</div>
								</div>
							</div>
	
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname">参与活动: </label> 
								<div class="col-md-10">
									<label class="radio-inline">
								  		<input type="radio" name="isFullcut" value="1" ${(goods.isFullcut==1)?string('checked','')}> 是
									</label>
									<label class="radio-inline">
								  		<input type="radio" name="isFullcut" value="0" ${(goods.isFullcut==0)?string('checked','')}> 否
									</label>
									<select id="activityid" name="activity.id"  class="form-control"  style="width:50%;display:${(goods.isFullcut==0)?string('none','block')};" >
										<option value="">请选择活动</option>
										<#list activityList as activity>
											<option value="${activity.id}"  ${(goods.activity.id==activity.id)?string('selected','')}>${activity.name}</option>
										</#list>
									</select>
								</div>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="cpname" >虚拟商品: </label> 
								<div class="col-md-10">
									<#-- 惠生活 -->
									<#if market.id==1>
									<label class="radio-inline">
								  		<input type="radio" name="isVirtual" value="1"  checked="checked" readonly> 是
									</label>
									<#-- 本地购或品牌购 -->
									<#elseif market.id==2 || market.id==3>
									<label class="radio-inline">
								  		<input type="radio" name="isVirtual" value="0" checked="checked" readonly> 否
									</label>
									<#-- 积分城 -->
									<#else>
									<label class="radio-inline">
								  		<input type="radio" name="isVirtual" value="1" ${(goods.isVirtual==1)?string('checked','')}> 是
									</label>
									<label class="radio-inline">
								  		<input type="radio" name="isVirtual" value="0" ${(goods.isVirtual==0)?string('checked','')}> 否
									</label>
									</#if>
								</div>
							</div>
							
							<div class="form-group" id="goodsVirtualStartTime" style="<#if market.id==1>display:block;<#else>display:none;</#if>">	
								<label class="control-label col-md-2 no-padding-right" for="cpname">开始日期:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<input id="starttime" name="goodsVirtual.startTime"  type="text"  value="${goods.goodsVirtual.startTime}"  style="width:50%;"/> 
									</div>
								</div>
							</div>
							
							<div class="form-group" id="goodsVirtualEndTime" style="<#if market.id==1>display:block;<#else>display:none;</#if>">	
								<label class="control-label col-md-2 no-padding-right" for="cpname">截至日期:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<input id="endtime" name="goodsVirtual.endTime"  type="text"  value="${goods.goodsVirtual.endTime}"  style="width:50%;"/> 
									</div>
								</div>
							</div> 
							
							
							<!-- 其他 -->
							<div class="hr hr-dotted" style="border-top: 2px dotted #AAA"></div>
							
								
							<!-- 商品详情富文本 --> 
						   	<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" >商品内容:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<textarea path="content" id="content" name="content"   style="width:100%;">${goods.content}</textarea>		
									</div>
								</div>
							</div>
							
							<!-- 上传单张图片 -->	
							<div class="form-group">
								<label class="control-label col-md-2 no-padding-right" for="picurl">封面图片:</label>
								<div class="col-md-10">
									<div class="clearfix">
										<div class="settingView">
											<img id="file_0_pic" src="${goods.picurl}"/>
											<input class="picinput"  type="file" name="file" id="file_0" data-id="file_0" data-img="file_0_pic"  data-txt="file_0_txt" />
										</div>
									</div>
								</div>
							</div>
							<input name="picurl" id="picurl" value="${goods.picurl}" type="hidden">
							
							<!-- 上传多张图片 -->	
							<input id="uppath" type="hidden" value="${path}/upload/pic/640/320">
							<div id="invisible-list" style="display:none;">
								<#list goods.goodsPicList as pic>
									<p id="invisible-item-${pic.id}">
										<!--
			 							<input type="hidden" id="id${pic.id}" name="goodsPicList[${pic.id}].id" value=""/>
			 							-->
										<input type="hidden" id="picurl${pic.id}" name="picList" value="${pic.picurl}" />
									</p>
								</#list>
							</div>
								
							<div class="form-group">  
								<label   class="col-md-2 control-label no-padding-right">商品详情图:</label>
								<div class="col-md-10">
									<ul id="visible-list" class="list-unstyled clearfix">
										<#list goods.goodsPicList as pic>
											<li id="visible-item-${pic.id}" data-num="${pic.id}">
												<div class="show-img-panel">
													<img onclick="openModal(this)" class="middle" src="${pic.picurl}" />
													<span class="remove-btn" data-num="${pic.id}" onclick="removePic(this)">
														<i class="fa fa-remove bigger-110 red "></i>
													</span>
						                		</div>
						                	</li>
						                </#list>	
				               		</ul>
								 	<input id="num" style="display: none;" value="">
									<label class="ace-file-input">
									 	<table id="fields-table" class="table table-striped table-bordered table-hover">
											<tr id="tr0">
												<td style="width: 420px;">
													<input class="form-control" type="file" id="id-input-file"   name="file"  onchange="addPic(this)" >
												  	<span style="width: 400px;margin-top: 10px;margin-left: 5px;"  class="ace-file-container"  data-title="上传">
														<span  class="ace-file-name">
															<p id="filename">点击这里上传文件</p><i class=" ace-icon fa fa-upload"></i>
														</span>
													</span>
													<a class="remove" href="#"><i class=" ace-icon fa fa-times"></i></a>
												</td>
												
												<td>
													<button type="button" data="0" class="btn btn-default dropdown-toggle" onclick="createView()">确定</button>
												</td>
										   </tr>
										</table>	
									</label>
								</div>  
							</div>
					
							
					
						
							
						
					
								
							<!-- 提交开始 -->	
		                    <div class="form-group" style="margin-top: 100px;">
		                    	<div class="col-md-8 center">
			                    	<button id="form-submit" type="button" onclick="mysubmit('validation-form')"  class="btn btn-primary">
										<i class="fa fa-save align-top bigger-125"></i>保存
									</button>
								 
									<button type="reset" class="btn">
										<i class="fa fa-pencil align-top bigger-125"></i>重置
									</button>
								</div>					
	                   		</div>
							<!-- 提交结束  -->
							
						</div>		
					</div>
				</form>
				<!-- 表单结束 -->	
		
	</div>		
	<!-- page-content end -->

</@htmlBody>



<@htmlBodyScript>
	<!-- 页面脚本 -->
	<script type="text/javascript" >
		
	 	$(function(){
	 		<!--是否启用库存 -->
 			$("input[name='isInventory']").click(function(){
	 			if($(this).val()==1){
	 				$("#remain").show();
	 			}else{
	 				$("#remain").hide();
	 			}
	 		});
	 		$("input[name='isPurRestrictions']").click(function(){
	 			if($(this).val()==1){
	 				$("#purRestrictionsNum").show();
	 			}else{
	 				$("#purRestrictionsNum").hide();
	 			}
	 		});
	 		
	 		<!--是否参加满减活动 -->
	 		$("input[name='isFullcut']").click(function(){
	 			if($(this).val()==1){
	 				$("#activityid").show();
	 			}else{
	 				$("#activityid").hide();
	 			}
	 		});
	 		
	 		<!--显示或隐藏打折内容 -->
	 		$("input[name='isDiscount']").click(function(){
	 			if($(this).val()==1){
	 				$("#discount").show();
	 			}else{
	 				$("#discount").hide();
	 			}
	 		});
	 		
	 		
	 		<!--是否为虚拟商品 -->
	 		$("input[name='isVirtual']").click(function(){
	 			if($(this).val()==1){
	 				$("#goodsVirtualStartTime").show();
	 				$("#goodsVirtualEndTime").show();
	 			}else{
	 				$("#goodsVirtualStartTime").hide();
	 				$("#goodsVirtualEndTime").hide();
	 			}
	 		});
	 		
	 		$("#originalPrice").blur(function(){
	 		if($("#promotionPrice").val()==""){
	 			$("#price").val($(this).val());
	 			}
	 		})
	 		
 			$("#promotionPrice").blur(function(){
 				if($("#discount").val()==""){
	 			$("#price").val($(this).val());
	 		}
	 		})
	 		
	 		$("#discount").blur(function(){
	 			var expression = /^(\d(\.\d)?|10)$/;
	 			
 				var price = $("#promotionPrice").val();
	 			if(price==''){
	 				return false;
	 			}
	 			var discount = $(this).val();
	 			if(!expression.test(discount)){
	 				alert("请输入0.1-9.9");
	 				return false;
	 			}	
	 			var prices = price*discount/10;
	 			$("#price").val(Math.round(prices*100)/100);
	 		})
	 		
	 	});
	</script>
	
	<!-- 日期控件-->
 	<link rel="stylesheet" href="${resources}/plugins/bootstrap-datetimepicker/2.3.4/css/bootstrap-datetimepicker.min.css" />
 	<script src="${resources}/plugins/bootstrap-datetimepicker/2.3.4/js/bootstrap-datetimepicker.js"></script>
   	<script src="${resources}/plugins/bootstrap-datetimepicker/2.3.4/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
	<script>
			$("#starttime,#endtime").datetimepicker({
				bootcssVer:3,
			    format: "yyyy-mm-dd",
			    language:"zh-CN",
				weekStart: 1,
	        	todayBtn:  1,
				autoclose: 1,
				todayHighlight: 1,
				startView: 2,
				minView: 2,
				forceParse: 0
			});
		</script>

	<!-- 富文本编辑器 -->
	<script charset="utf-8" src="${resources}/plugins/kindeditor/4.1.11/kindeditor-all-min.js"></script>
	<script charset="utf-8" src="${resources}/plugins/kindeditor/4.1.11/lang/zh-CN.js"></script>
    <!-- 实例化编辑器 -->
    <script type="text/javascript">
		var editor;
	    KindEditor.ready(function(K) {
	            editor = K.create('#content', {
	                    resizeType : 1,
	                    pasteType  : 1,
	                    uploadJson : '${path}/richtext/upload/pic/0/0',
	        		    height : "350px",
	        		    afterSetData : function(id) {}
	            });
	 	});
    </script>

	

	<!-- 上传控件 -->
	<script src="${resources}/plugins/fileupload/ajaxfileupload.js"></script>
	<!-- 多张 -->
	<script src="${resources}/plugins/fileupload/upload.multiple.js"></script>
	<!-- 单张 -->
	<script src="${resources}/plugins/fileupload/upload.single.js"></script>
 	<script>
 		//上传图片
		$(".picinput").uploadPreview({
			Width: 200,
			Height: 200,
			IsCompression: true,
			Url: "${path}/upload/pic",
			Success: showSuccess,
			Error:showError,
			
		});
		//回调函数
		function showSuccess(id,url){
			$("#"+id).val(url);
			$("#picurl").val(url);
		}
		function showError(id,error){
			alert(error);
		}
		
		$("#categoriesid").change(function(){
			var pid = $(this).find("option:selected").val();
			if(pid==""){
				//alert("请选择父节点");
				return;
			}
			$.ajax({
				url:"${path}/manage/commodity/goods/ajaxChild?pid="+pid,
				type:"post",
				success:function(data){
					var html = "<option value=''>请选择</option>";
					if(data.length>0){
						for(var i=0;i<data.length;i++){
							html+="<option value='"+data[i].id+"'>"+data[i].name+"</option>";						}
					}
					$("#categoryid").html(html);
				}
			})
		})
		
	</script>


	<!-- 验证框架 -->	
	<script src="${resources}/plugins/jquery-validation/jquery.validate.min.js"></script>
	<script src="${resources}/plugins/jquery-validation/messages_zh.min.js"></script>
	
	<script type="text/javascript">
				
			jQuery.validator.addMethod("isName", function(value, element) {       
	 			var corpName =  /^[\u0391-\uFFE5\w\-\s\.]{1,50}$/;
  				 return this.optional(element) || corpName.test(value);       
 			}, "请使用汉字、英文字母、数字、下划线、减号、空格或点");   
 			
			jQuery.validator.addMethod("isPrice", function(value, element) {       
	 			var price =  /^[0-9]+(.[0-9]{0,2})?$/;
  				 return this.optional(element) || price.test(value);       
 			}, "请输入整数或至多两位小数");   
			
			$('#validation-form').validate({
				errorElement: 'div',
				errorClass: 'help-block',
				focusInvalid: false,
				rules: {
					name: {
						required:true,
						maxlength:50
					},
					title: {
						required:true,
						maxlength:100
					},
					picurl: {
						required:true
					},
					price: {
						required:true
					},
					originalPrice: {
						required:true,
						isPrice:true
					},
					promotionPrice:{
						required:true,
						isPrice:true
					},
					remain:{
						required:true,
						digits:true
					},
					"activity.id": {
						required:true
					},
					"category.id": {
						required:true
					},
					"goodsVirtual.startTime": {
						required:true
					},
					"goodsVirtual.endTime": {
						required:true
					}
				},
		
				messages: {
				},
		
		
				highlight: function (e) {
					$(e).closest('.form-group').removeClass('has-info').addClass('has-error');
				},
		
				success: function (e) {
					$(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
					$(e).remove();
				},
		
				errorPlacement: function (error, element) {
					if(element.is('input[type=checkbox]') || element.is('input[type=radio]')) {
						var controls = element.closest('div[class*="col-"]');
						if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
						else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
					}
					else if(element.is('.select2')) {
						error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
					}
					else if(element.is('.chosen-select')) {
						error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
					}
					else error.insertAfter(element.parent());
				},
		
				invalidHandler: function (form) {
				}
			});
			
			//上传图片
			$(".picinput").uploadPreview({
				Width: 200,
				Height: 200,
				IsCompression: true,
				Url: "${path}/upload/pic",
				Success: showSuccess,
				Error:showError,
				
			});
			//回调函数
			function showSuccess(id,url){
				$("#"+id).val(url);
				$("#picurl").val(url);
			}
			
			function showError(id,error){
				alert(error);
			}
			
	</script>
	
	
	<script>
	    /**
	     * 功能 : 提交表单
		 */
	 	function mysubmit(form){
	 		editor.sync();
	 		$("#"+form).submit();
	 	}


   //选择商品副分类
 	  <#if market.id==4> 
      $("#categoryid").change(function(){
         
          var selectVal = $(this).find("option:selected").text();
          if(selectVal == "积分商城"){
             $("#limitType").val(0);
          }
            if(selectVal == "限时抢"){
              $("#limitType").val(1);
          }
            if(selectVal == "限量抢"){
             $("#limitType").val(2);
          }
      
      });
       </#if>
      
	</script>
</@htmlBodyScript>


